<template>
  <div>
    <el-card>
      <el-page-header icon="" title="产品管理">
        <template #content>
          <span class="text-large font-600 mr-3" style="font-size: 16px">
            产品列表
          </span>
        </template>
      </el-page-header>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="title" label="产品名称" width="180" />
        <el-table-column prop="introduction" label="简要描述">
        </el-table-column>
        <el-table-column prop="editTime" label="更新时间">
          <template #default="scope">
            {{ dayjs(scope.row.editTime).format("YYYY年MM月DD日") }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button circle :icon="Edit" @click="handleEdit(scope.row)">
            </el-button>
            <el-popconfirm
              title="你确定删除吗"
              cancel-button-text="取消"
              confirm-button-text="确定"
              @confirm="handleDelete(scope.row)"
            >
              <template #reference>
                <el-button circle :icon="Delete" type="danger"> </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import axios from "axios";
import { dayjs } from "element-plus";
import "dayjs/locale/zh-cn";
import { Edit, Delete } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
const router = useRouter();

const tableData = ref([]);

onMounted(() => {
  // 设置语言为中文
  dayjs.locale("zh-cn");
  getTableData();
});

const getTableData = async () => {
  const res = await axios.get("/adminapi/product/list");
  // console.log(res.data.result.list);
  tableData.value = res.data.result.list;
};

//删除回调
const handleDelete = async (item) => {
  await axios.delete(`/adminapi/product/list/${item.id}`);
  await getTableData();
};

//编辑回调
const handleEdit = (item) => {
  router.push(`/product-manage/editproduct/${item.id}`);
};
</script>

<style scoped>
.el-table {
  margin-top: 50px;
}
::v-deep .htmlcontent {
  img {
    max-width: 100%;
  }
}
</style>
